﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TuringVM.Core
{
    public class Machine
    {
        private CommandSet _commands;
        private MachineSettings _settings;

        private Tape _code = new Tape();
        private Tape _data = new Tape();

        public Machine(CommandSet commands, MachineSettings settings)
        {
            _commands = commands;
            _settings = settings;
        }

        public delegate void Debug(CommandSet commands, Tape code, Tape data);

        public Debug Debugger { get; set; }

        public void Load(IEnumerable<byte> code)
        {
            _code.Fill(code);
        }

        public void Run()
        {
            byte result = 0;
            while (result == 0 && _code.Value != 0)
            {
                if (_settings.AllowDebugging && Debugger != null)
                {
                    Debugger(_commands, _code, _data);
                }
                result = _commands[_code.Value].Execute(_code, _data);
                _code.MoveBy(1);
            }

        }
    }
}
